package com.neu.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.neu.pojo.ClazzOption;
import com.neu.pojo.Emp;
import com.neu.pojo.JobOption;
import com.neu.pojo.Result;
import com.neu.service.EmpService;
import com.neu.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/report")
@RestController
public class ReportController {

    @Autowired
    private ReportService reportService;
    @Autowired
    private EmpService empService;

//    @GetMapping("/empJobData")
//    public Result getEmpJobData(){
//        log.info("统计员工职位人数");
//        JobOption jobOption = reportService.getEmpJobData();
//        return Result.success(jobOption);
//
//    }
    @GetMapping("/empJobData")
    public Result getEmpJobData(){
        log.info("统计员工职位人数");
        List<Emp> list = empService.findAll();
        Map<Integer, Long> collect = list.stream().filter(x -> ObjectUtil.isNotEmpty(x.getJob()))
                .collect(Collectors.groupingBy(Emp::getJob, Collectors.counting()));
        //按照 job 字段的值分组，并统计每组的数量。
        List<String> xAxis = new ArrayList<>();
        List<Long> yAxis = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(collect)){
            for(Integer key: collect.keySet()){
                if(key == 1) {
                    xAxis.add("班主任");
                } else if (key == 2) {
                    xAxis.add("讲师");
                } else if (key == 3) {
                    xAxis.add("学工主管");
                } else if (key == 4) {
                    xAxis.add("教研主管");
                } else if (key == 5) {
                    xAxis.add("咨询师");
                }else{
                    xAxis.add("其他");
                }
                yAxis.add(collect.get(key));

            }

        }

        JobOption jobOption = new JobOption(xAxis,yAxis);

        return Result.success(jobOption);

    }
//    //统计员工性别人数
//    @GetMapping("/empGenderData")
//    public Result getEmpGenderData(){
//        log.info("统计员工性别人数");
//        List<Map<String,Object>> genderList = reportService.getEmpGenderData();
//        return Result.success(genderList);
//    }

    //统计员工性别人数
    @GetMapping("/empGenderData")
    public Result getEmpGenderData(){
        log.info("统计员工性别人数");
        List<Emp> list = empService.findAll();
        Map<Integer, Long> collect = list.stream().filter(x -> ObjectUtil.isNotEmpty(x.getGender()))
                .collect(Collectors.groupingBy(Emp::getGender, Collectors.counting()));
        List<Map<String,Object>> mapList = new ArrayList<>();
//        for (Map.Entry<Integer, Long> entry : collect.entrySet()) {
//            Map<String,Object> map = new HashMap<>();
//            if(entry.getKey() == 1){
//                map.put("name","男性员工");
//            }else{
//                map.put("name","女性员工");
//            }
////            map.put("name",entry.getKey());
//            map.put("value",entry.getValue());
//            mapList.add(map);
//        }
        for (Integer key : collect.keySet()) {
            Map<String,Object> map = new HashMap<>();
            if(key == 1){
                map.put("name","男性员工");
            }else{
                map.put("name","女性员工");
            }
//            map.put("name",entry.getKey());
            map.put("value",collect.get(key));
            mapList.add(map);
        }
        return Result.success(mapList);
    }

    @GetMapping("/studentDegreeData")
    public Result getStudentDegreeData(){
        log.info("统计学生学历人数");
        List<Map<String,Object>> degreeList = reportService.getStudentDegreeData();
        return Result.success(degreeList);
    }

    @GetMapping("/studentCountData")
    public Result getStudentCountData(){
        log.info("统计学生班级人数");
        ClazzOption clazzOption = reportService.getClazzCountData();
        return Result.success(clazzOption);
    }
}
